/*
* $Id: NavigationWin.cpp 195 2010-11-28 01:47:52Z volok-aleksej@yandex.ru $
*
* Copyright 2008-2009 by Volokitin Alexey
* All rights reserved
*
*/
#include "Common/include.h"
#include "NavigationWin.h"
#include "Event/EventId.h"
#include "Components/Journal/FamilyManager.h"

#define MENU_ADD_POINT_ID       0x1
#define MENU_DELETE_POINT_ID    0x2
#define MENU_CHANGE_POINT_ID    0x3
#define MENU_ADD_FAMILY_ID      0x4
#define MENU_CHANGE_FAMILY_ID   0x5
#define MENU_DELETE_FAMILY_ID   0x6
#define MENU_ADD_INSPECT_ID     0x7
#define MENU_ADD_QUEEN_BEE_ID   0x8

CNavigationWin::CNavigationWin(){
	IEventManager::GetEventManager()->SubscribeEvent(this, POINTS_DB_CHANGE_EVENT);
	IEventManager::GetEventManager()->SubscribeEvent(this, FAMILIES_DB_CHANGE_EVENT);

	m_pImageList = new CImageList;
	m_pImageList->Create(16, 16, ILC_COLOR32, 2, 0);

	m_pImageList->Add(LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_POINT)));
	m_pImageList->Add(LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_FAMILY)));
}
CNavigationWin::~CNavigationWin(){
	m_pImageList->DeleteImageList();
	delete m_pImageList;
	IEventManager::GetEventManager()->UnSubscribeEvent(this, POINTS_DB_CHANGE_EVENT);
	IEventManager::GetEventManager()->UnSubscribeEvent(this, FAMILIES_DB_CHANGE_EVENT);
}

void CNavigationWin::WindowCreate(CWnd* pParentWin){
	Create(WS_CHILD | WS_VISIBLE | WS_BORDER | TVS_HASBUTTONS | TVS_LINESATROOT |
			TVS_EDITLABELS | TVS_FULLROWSELECT | TVS_SHOWSELALWAYS,
			CRect(0,0,0,0), pParentWin, TREE_NAVIGATION_WND_ID);
	SetImageList(m_pImageList, TVSIL_NORMAL);
	Reload();
}

void CNavigationWin::EventHandler(){
	Reload();
}
/************************************************************************/
/*                        Message Function                              */
/************************************************************************/
BEGIN_MESSAGE_MAP(CNavigationWin, CTreeCtrl)
	ON_WM_RBUTTONDOWN()
	ON_WM_LBUTTONDOWN()
	ON_WM_CONTEXTMENU()
	ON_COMMAND(MENU_ADD_POINT_ID, OnAddPoint)
	ON_COMMAND(MENU_ADD_FAMILY_ID, OnAddFamily)
	ON_COMMAND(MENU_ADD_INSPECT_ID, OnAddInspect)
	ON_COMMAND(MENU_ADD_QUEEN_BEE_ID, OnAddQueenBee)
	ON_COMMAND(MENU_CHANGE_POINT_ID, OnChangePoint)
	ON_COMMAND(MENU_CHANGE_FAMILY_ID, OnChangeFamily)
	ON_COMMAND(MENU_DELETE_FAMILY_ID, OnDeleteFamily)
	ON_COMMAND(MENU_DELETE_POINT_ID, OnDeletePoint)
END_MESSAGE_MAP()

void CNavigationWin::OnRButtonDown(UINT nFlags, CPoint point){
	HTREEITEM htreeItem = HitTest(point);
	SelectItem(htreeItem);
	CreateMenu(point);
//	__super::OnRButtonDown(nFlags, point);
}
void CNavigationWin::OnLButtonDown(UINT nFlags, CPoint point){
	__super::OnLButtonDown(nFlags, point);
	HTREEITEM htreeItem = HitTest(point);
	SelectItem(htreeItem);
}
void CNavigationWin::OnContextMenu(CWnd* pWnd, CPoint pos){
	HTREEITEM item = GetSelectedItem();
	CRect rect;
	if(item){
		GetItemRect(item, rect, false);
		CreateMenu(CPoint(rect.Width()/2, rect.top + rect.Height()/2));
	}else{
		GetClientRect(rect);
		CreateMenu(CPoint(rect.Width()/2, 0));
	}
}

void CNavigationWin::OnAddPoint(){
	ItemManipulation(Journal::ADD_POINT);
}
void CNavigationWin::OnAddFamily(){
	ItemManipulation(Journal::ADD_FAMILY);
}
void CNavigationWin::OnAddInspect(){
	ItemManipulation(Journal::ADD_INSPECT);
}
void CNavigationWin::OnAddQueenBee(){
	ItemManipulation(Journal::ADD_QUEEN_BEE);
}
void CNavigationWin::OnChangePoint(){
	ItemManipulation(Journal::CHANGE_POINT);
}
void CNavigationWin::OnChangeFamily(){
	ItemManipulation(Journal::CHANGE_FAMILY);
}
void CNavigationWin::OnDeletePoint(){
	ItemManipulation(Journal::DELETE_POINT);
}
void CNavigationWin::OnDeleteFamily(){
	ItemManipulation(Journal::DELETE_FAMILY);
}


/************************************************************************/
/*                          User Function                               */
/************************************************************************/
void CNavigationWin::Reload(){
	DeleteAllItems();
	CFamilyManager* pManager = IJournalComponent::GetFamilyManager();
	std::map<int, CFamilyManager::point> aPoints = pManager->GetPoints();
	for(std::map<int, CFamilyManager::point>::iterator it = aPoints.begin(); it != aPoints.end(); it++){
		HTREEITEM item = InsertItem(it->second.m_strName.c_str(), 0, 0);
		SetItemData(item, MAKELONG(Journal::POINT, it->first));
		std::map<int, CFamilyManager::family> aFamilies = pManager->GetFamilies(it->first);
		for(std::map<int, CFamilyManager::family>::iterator it1 = aFamilies.begin(); it1 != aFamilies.end(); it1++){
			HTREEITEM item1 = InsertItem(it1->second.m_strName.c_str(), 1, 1, item);
			SetItemData(item1, MAKELONG(Journal::FAMILY, it1->first));
		}
	}
}
void CNavigationWin::SetMenuItem(CMenu* menu, int nNum, int nId, const std::tstring& strText, BOOL bSep)
{
	MENUITEMINFO mii;
	mii.cbSize = sizeof(MENUITEMINFO);
	mii.fMask = MIIM_DATA | MIIM_STATE | MIIM_TYPE | MIIM_ID;
	mii.fState = MFS_ENABLED;
	if(!bSep)
	{
		mii.fType = MFT_STRING;
		mii.wID = nId;
		mii.dwTypeData = (TCHAR*)strText.c_str();
	}
	else
		mii.fType = MFT_SEPARATOR;
	menu->InsertMenuItem(nNum, &mii);
}
void CNavigationWin::CreateMenu(CPoint pos)
{
	CMenu menu;
	HTREEITEM item = GetSelectedItem();
	if(menu.CreatePopupMenu())
	{
		long p = 0;
		if(item)
			p = (long)GetItemData(item);
		if(!item || !GetItemData(item))
			SetMenuItem(&menu, 0, MENU_ADD_POINT_ID, ILanguageManager::GetLanguageManager()->GetString(ADD_POINT_ID), FALSE);
		else if(LOWORD(p) == Journal::POINT)
		{
			SetMenuItem(&menu, 0, MENU_ADD_FAMILY_ID, ILanguageManager::GetLanguageManager()->GetString(ADD_FAMILY_ID), FALSE);
			SetMenuItem(&menu, 1, MENU_CHANGE_POINT_ID, ILanguageManager::GetLanguageManager()->GetString(CHANGE_POINT_ID), FALSE);
			SetMenuItem(&menu, 2, MENU_DELETE_POINT_ID, ILanguageManager::GetLanguageManager()->GetString(DELETE_POINT_ID), FALSE);
		}
		else if(LOWORD(p) == Journal::FAMILY)
		{
			SetMenuItem(&menu, 0, MENU_ADD_INSPECT_ID, ILanguageManager::GetLanguageManager()->GetString(ADD_INSPECT_ID), FALSE);
			SetMenuItem(&menu, 1, MENU_ADD_QUEEN_BEE_ID, ILanguageManager::GetLanguageManager()->GetString(ADD_QUEEN_BEE_ID), FALSE);
			SetMenuItem(&menu, 2, MENU_CHANGE_FAMILY_ID, ILanguageManager::GetLanguageManager()->GetString(CHANGE_FAMILY_ID), FALSE);
			SetMenuItem(&menu, 3, MENU_DELETE_FAMILY_ID, ILanguageManager::GetLanguageManager()->GetString(DELETE_FAMILY_ID), FALSE);
		}
		CRect rect;
		GetWindowRect(rect);
		menu.TrackPopupMenu(TPM_LEFTALIGN|TPM_LEFTBUTTON, rect.left + pos.x, rect.top + pos.y, this);
	}
}

void CNavigationWin::ItemManipulation(Journal::typeItemMnpl item){
	IWnd* pWnd = IVisualManager::GetVisualManager()->GetVisualComponent(
		ILanguageManager::GetLanguageManager()->GetString(JOURNAL_CAPTION_ID))->GetComponentFrame();
	HTREEITEM nSelItem = GetSelectedItem();
	std::tstring strTitle, 
	strText = ILanguageManager::GetLanguageManager()->GetString(QUESTION_DELETE_ID) + _T(" '") + GetItemText(nSelItem).GetString() + _T("'?");
	if(item == Journal::DELETE_POINT)
		strTitle = ILanguageManager::GetLanguageManager()->GetString(DELETE_POINT_ID);
	else if(item == Journal::DELETE_FAMILY)
		strTitle = ILanguageManager::GetLanguageManager()->GetString(DELETE_FAMILY_ID);
	if((item == Journal::DELETE_POINT || item == Journal::DELETE_FAMILY) &&
	dynamic_cast<CWnd*>(pWnd)->MessageBox(strText.c_str(),strTitle.c_str(), MB_YESNO) == IDNO)
		return;
	long p = 0;
	if(nSelItem)
		p = (long)GetItemData(nSelItem);
	pWnd->Send(UM_ITEM_MANIPULATION, item, HIWORD(p));
	if(item == Journal::DELETE_POINT || item == Journal::DELETE_FAMILY)
		DeleteItem(nSelItem);
}
